<?php

require_once('lib/gymnasium.php');


?>
<h3>Modifier un gymnase</h3>
<?php

$id = intval($_GET['id']);
$gym = gym_get_by_id($id);
if ($gym == NULL) {
	?>
	<p class="error">
		Le gymnase demandé n'existe pas
	</p>
	<?php
}
else {

	if (isset($_POST['submit-gym'])) {
		$name = clean_str($_POST['name']);
		$address = clean_str($_POST['address']);
		$postal_code = clean_str($_POST['postal_code']);
		$city = clean_str($_POST['city']);
		$description = clean_str_preserve_tags($_POST['description']);
		$googlemap_url = clean_str_preserve_tags($_POST['googlemap_url']);
		
		$errors = array();
		if ($name == '') {
			$errors[] = 'Vous devez indiquer un nom';
		}
		if ($address == '') {
			$errors[] = 'Vous devez indiquer une addresse';
		}
		if ($postal_code == '') {
			$errors[] = 'Vous devez indiquer un code postal';
		}
		if ($city == '') {
			$errors[] = 'Vous devez indiquer la ville';
		}
		if ($description == '') {
			$errors[] = 'Vous devez indiquer une description du gymnase';
		}
		if ($googlemap_url == '') {
			$errors[] = 'Vous devez indiquer le plan d\'accès google map';
		}
		
		
		if (count($errors) == 0) {
			gym_update($id, $name, $address, $postal_code, $city, $description, $googlemap_url);
			echo '
			<p class="success">
				Le gymnase a été mis à jour !<br />
				<a href="?p=club&amp;r=gym_list">Revenir</a> à la liste des gymnases
			</p>';
		}
		else {
			?>
			<p class="error">Merci de corriger les erreurs suivantes : </p>
			<?php
			print_array($errors);
		}
	}
	else {
		$name = $gym['name'];
		$address = $gym['address'];
		$postal_code = $gym['postal_code'];
		$city = $gym['city'];
		$description = $gym['description'];
		$googlemap_url = $gym['googlemap_url'];
	}


	?>

	<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post" class="form-content">
		<table>
			<tr>
				<td>Nom : </td>
				<td><input type="text" name="name" value="<?php echo $name ?>" /></td>
			</tr>
			<tr>
				<td>Adresse : </td>
				<td><input type="text" name="address" value="<?php echo $address ?>" /></td>
			</tr>
			<tr>
				<td>Code postal : </td>
				<td><input type="text" name="postal_code" value="<?php echo $postal_code ?>" /></td>
			</tr>
			<tr>
				<td>Ville : </td>
				<td><input type="text" name="city" value="<?php echo $city ?>" /></td>
			</tr>
			<tr>
				<td>Description : </td>
				<td>
					<textarea name="description" rows="8" cols="50"><?php echo $description ?></textarea>
				</td>
			</tr>
			<tr>
				<td>Plan d'accès google map : <br/>(copiez/collez le code HTML)</td>
				<td>
					<textarea name="googlemap_url" rows="8" cols="50"><?php echo $googlemap_url ?></textarea>
				</td>
			</tr>
		</table>
		<p>
			<input type="submit" name="submit-gym" value="Valider" />
		</p>
	</form>
	<?php
}


